/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.handeasy.session;

import com.handeasy.entity.MarkerEntity;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Aurelie
 */
@Stateless
@WebService
public class MarkerSessionBean {
    @PersistenceContext(unitName="HandEasy-ejbPU")
    private EntityManager em;
 
    /**
     * Methode qui permet de creer un marker et qui sera visible sur la map du telephone
     * @param lLatitude
     * @param lLongitude
     * @param lNameMarker
     * @param lNumberAddress
     * @param lStreet
     * @param lPostalCode
     * @param lCity
     * @param lCountry
     * @param lNameCategory
     * @param lNameAccess
     * @param lRestroom
     * @param lStaff
     * @param lPhoto
     * @return 
     */
    @WebMethod(operationName = "createMarker")
    public MarkerEntity createMarker(@WebParam(name = "latitude")double lLatitude, @WebParam(name = "longitude")double lLongitude, @WebParam(name = "nameMarker")String lNameMarker, @WebParam(name = "numberAddress")String lNumberAddress, @WebParam(name = "street")String lStreet, @WebParam(name = "postalCode")String lPostalCode, @WebParam(name = "city")String lCity, @WebParam(name = "country")String lCountry, @WebParam(name = "nameCategory")String lNameCategory, @WebParam(name = "nameAccess")String lNameAccess, @WebParam(name = "restroom")String lRestroom, @WebParam(name = "staff")String lStaff, @WebParam(name="photo")String lPhoto) {
        MarkerEntity m = new MarkerEntity(lLatitude, lLongitude, lNameMarker, lNumberAddress, lStreet, lPostalCode, lCity, lCountry, lNameCategory, lNameAccess, lRestroom, lStaff, lPhoto);
        em.persist(m);
       
        return m;
    }
    
    /**
     * Méthode qui renvoit la liste de tous les markers existant
     * @return 
     */
    @WebMethod(operationName= "getAllMarker")
      public List<MarkerEntity> getAllMarker() {
        Query q = em.createQuery("select m from MarkerEntity m");
        return q.getResultList();
    }
    
    /**
     * Methode qui renvoit la liste de toutes les latitudes des markers existants
     * @return 
     */
    @WebMethod(operationName= "getAllMarkerLat")
    public String getAllMarkerLat() {
         Query q = em.createQuery("select m.mLatitude from MarkerEntity m");
         return q.getResultList().toString();
    }
    
    /**
     * Methode qui renvoit la liste de toutes les longitudes des markers existants
     * @return 
     */
    @WebMethod(operationName= "getAllMarkerLng")
    public String getAllMarkerLng() {
         Query q = em.createQuery("select m.mLongitude from MarkerEntity m");
         return q.getResultList().toString();
    }
   
    /**
     * Méthode qui recupere l'id du marker suivant sa latitude et longitude
     * @param lLatitude
     * @param lLongitude
     * @return 
     */
    @WebMethod(operationName = "getMarkerIdByLatLng")
    public Long getMarkerIdByLatLng(@WebParam(name = "latitude") Double lLatitude, @WebParam(name = "longitude") Double lLongitude) {
        Query q = em.createQuery("select m.id from MarkerEntity m where m.mLatitude ='" + lLatitude + "' AND  m.mLongitude = '" + lLongitude + "'");
        return Long.parseLong(q.getSingleResult().toString());
        
    }
     /**
     * Methode qui renvoie les résultats d'une recherche
     * @param lReq
     * @return 
     */
    @WebMethod(operationName= "getMarkerSearch")
    public List<MarkerEntity> getMarkerSearch(@WebParam(name = "request")String lReq) {      
        
         Query q = em.createQuery("select m from MarkerEntity m where "
                 + "UPPER(m.mName) like :lReq");
         q.setParameter("lReq", "%" + lReq.toUpperCase() + "%");
         System.out.println("GETMARKERSEARCH size : " + q.getResultList().size() + " req : " + lReq);
         return q.getResultList();
    }   
    
    @WebMethod(operationName = "getCategoryByLatLng")
    public String getCategoryByLatLng(@WebParam(name = "latitude") Double lLatitude, @WebParam(name = "longitude") Double lLongitude) {
        Query q = em.createQuery("select m.mNameCategory from MarkerEntity m where m.mLatitude ='" + lLatitude + "' AND  m.mLongitude = '" + lLongitude + "'");
        return q.getSingleResult().toString();
        
    }
    
    @WebMethod(operationName = "getAccessByLatLng")
    public String getAccessByLatLng(@WebParam(name = "latitude") Double lLatitude, @WebParam(name = "longitude") Double lLongitude) {
        Query q = em.createQuery("select m.mNameAccess from MarkerEntity m where m.mLatitude ='" + lLatitude + "' AND  m.mLongitude = '" + lLongitude + "'");
        return q.getSingleResult().toString();
        
    }
    
    @WebMethod(operationName = "getStaffByLatLng")
    public String getStaffByLatLng(@WebParam(name = "latitude") Double lLatitude, @WebParam(name = "longitude") Double lLongitude) {
        Query q = em.createQuery("select m.mStaff from MarkerEntity m where m.mLatitude ='" + lLatitude + "' AND  m.mLongitude = '" + lLongitude + "'");
        return q.getSingleResult().toString();
        
    }
    
    @WebMethod(operationName = "getRestroomByLatLng")
    public String getRestroomByLatLng(@WebParam(name = "latitude") Double lLatitude, @WebParam(name = "longitude") Double lLongitude) {
        Query q = em.createQuery("select m.mRestroom from MarkerEntity m where m.mLatitude ='" + lLatitude + "' AND  m.mLongitude = '" + lLongitude + "'");
        return q.getSingleResult().toString();
        
    }
    
    @WebMethod(operationName = "getPhotoByLatLng")
    public String getPhotoByLatLng(@WebParam(name = "latitude") Double lLatitude, @WebParam(name = "longitude") Double lLongitude) {
        Query q = em.createQuery("select m.mPhoto from MarkerEntity m where m.mLatitude ='" + lLatitude + "' AND  m.mLongitude = '" + lLongitude + "'");
        return q.getSingleResult().toString();
        
    }
  
    @WebMethod(operationName = "insertPhotoInMarker")
    public void insertPhotoInMarker(@WebParam(name = "photo")String lPhoto, @WebParam(name = "latitude")Double lLatitude, @WebParam(name = "longitude")Double lLongitude) {
        Query q = em.createQuery("UPDATE MarkerEntity m SET m.mPhoto ='" + lPhoto + "' where m.mLatitude ='" + lLatitude + "' AND  m.mLongitude = '" + lLongitude + "'");
   q.executeUpdate();
    }

}
